package net.lainiao;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock;

import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2016/11/10.
 */
public class ExampleClientReadWriteLocks {
    private final InterProcessReadWriteLock lock;
    private final InterProcessMutex readLock;
    private final InterProcessMutex writeLock;
    private final FakeLimitedResource resource;
    private final String clientName;

    public ExampleClientReadWriteLocks(CuratorFramework client,String lockPath,FakeLimitedResource resource,String clientName){
        this.resource=resource;
        this.clientName=clientName;
        lock=new InterProcessReadWriteLock(client,lockPath);
        readLock=lock.readLock();
        writeLock=lock.writeLock();
    }

    public void doWork(long time, TimeUnit unit) throws Exception {
        if(!writeLock.acquire(time,unit)){
            throw new IllegalStateException(clientName+" could not acquire the writeLock");
        }
        System.out.println(clientName+" has the writeLock");
        if(!readLock.acquire(time,unit)){
            throw new IllegalStateException(clientName+" could not acquire the readLock");
        }
        System.out.println(clientName+" has the readLock too");
        try {
            resource.use();
        } finally {
            System.out.println(clientName+" releasing the lock");
            readLock.release();
            writeLock.release();
        }
    }
}
